# Maintainer: Kyle Kauffman <kyle.j.kauffman@gmail.com>

_realname=ipython
pkgbase=mingw-w64-python2-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
pkgver=5.8.0
pkgrel=1
pkgdesc="An enhanced Interactive Python shell (mingw-w64)"
arch=('any')
url="https://ipython.org/"
license=('BSD')
install=${_realname}2-${CARCH}.install
depends=('winpty'
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-python2-traitlets"
         "${MINGW_PACKAGE_PREFIX}-python2-simplegeneric"
         "${MINGW_PACKAGE_PREFIX}-python2-pickleshare"
         "${MINGW_PACKAGE_PREFIX}-python2-prompt_toolkit1"
         "${MINGW_PACKAGE_PREFIX}-python2-backports.shutil_get_terminal_size"
         "${MINGW_PACKAGE_PREFIX}-python2-jedi"
         "${MINGW_PACKAGE_PREFIX}-python2-win_unicode_console")
optdepends=("${MINGW_PACKAGE_PREFIX}-python2-nose: for IPython's test suite"
            "${MINGW_PACKAGE_PREFIX}-python2-pyqt4: for ipython qtconsole"
            "${MINGW_PACKAGE_PREFIX}-python2-sip: for ipython qtconsole"
            "${MINGW_PACKAGE_PREFIX}-python2-pygments: for ipython qtconsole"
            "${MINGW_PACKAGE_PREFIX}-python2-jinja: for ipython notebook"
            "${MINGW_PACKAGE_PREFIX}-python2-pyzmq: ipython notebook and ipython qtconcole")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/ipython/ipython/archive/${pkgver}.tar.gz"
        0001-unimplemented-readline-calls.patch)
sha256sums=('b6e78abfb190514b2dda6f937863d1b66cbe28c8ebd87099ddacfaf1a3c6b714'
            'cd6dac2442a2477c28096d87cdb7f38ef46b9d3341920d9e88396288a81bfbe2')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}
# end helper functions

prepare() {
  cd "${srcdir}"
  pushd "${_realname}-${pkgver}"
    apply_patch_with_msg 0001-unimplemented-readline-calls.patch
    find ./ -type f -exec sed -i -e 's/prompt_toolkit/prompt_toolkit1/g' {} \;
  popd

  rm -rf "python2-build-${CARCH}" | true
  cp -r "${_realname}-${pkgver}" "python2-build-${CARCH}"
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
}

build() {
    msg "Python 2 build for ${CARCH}"  
    cd "${srcdir}/python2-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python2 setup.py build
}


package() {
  cd "${srcdir}/python2-build-${CARCH}"

  # see https://github.com/ipython/ipython/issues/2057
  #export LC_ALL=en_US.UTF-8
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install \
    --prefix=${MINGW_PREFIX} --root=${pkgdir} --optimize=1 --skip-build

  find "${pkgdir}/" -name "*.pyc" -delete
  find "${pkgdir}/" -type d -empty -delete

  # setuptools doesnt see mingw-w64-python's readline
  find "${pkgdir}/" -name 'requires.txt' -delete

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.cmd; do
    sed -e "s|${PREFIX_WIN}/bin/||g" -i ${_f}
  done

# For the command to work on the bash prompt, you must use
# winpty so include that in the Shebang before the python command.
# I don't like this but what I like really doesn't matter.
  for _f in iptest iptest2 ipython ipython2; do
     sed -e "s|/usr/bin/env |${MINGW_PREFIX}/|g" \
         -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
         -e "s|#!|#!winpty |" -i "${pkgdir}${MINGW_PREFIX}/bin/${_f}"
  done


  for file_script in iptest ipython; do
    rm ${pkgdir}${MINGW_PREFIX}/bin/${file_script}{,.cmd}
  done

  install -Dm644 docs/source/about/license_and_copyright.rst "${pkgdir}${MINGW_PREFIX}/share/licenses/ipython2/LICENSE"

  # Fix bin collisions
  find "${pkgdir}${MINGW_PREFIX}/bin/" -type f -regex '.*[^2].exe$' -delete
  find "${pkgdir}${MINGW_PREFIX}/bin/" -type f -regex '.*[^2]-script.py$' -delete
  find "${pkgdir}${MINGW_PREFIX}/bin/" -type f -regex '.*[^2].exe.manifest$' -delete

  # Fix manpage collisions
  cd "${pkgdir}${MINGW_PREFIX}/share/man/man1/"
  for i in *; do
    mv ${i} ${i/%.1/2.1}
  done

}
